//
//  MainHeaderCell.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/27.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "MainHeaderCell.h"
#import "Common.h"
#import "DataSingleton.h"
#import "DoorMessage.h"
#import "MBProgressHUD+MJ.h"







@implementation MainHeaderCell



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}




- (void)awakeFromNib {
    

    
    self.datasingleton = [DataSingleton sharedDataSingleton];
    
    
    
    self.backgroundColor = BACKGROUND2;
    //设置头像
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth/2-38, 30, 76, 76)];
    imageView.layer.cornerRadius = 38;
    imageView.layer.masksToBounds = YES;
    //设置边框以及颜色
    [imageView.layer setBorderWidth:2];
    imageView.image = [UIImage imageNamed:@"longin_default"];
    [imageView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
    
    
    UILabel *lbl = [[UILabel alloc]init];
    
    
    
    lbl.frame =CGRectMake(ViewWidth/2-50, 105, 100, 40);

    lbl.text = [NSString stringWithFormat:@"%@",self.datasingleton.name];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.font = [UIFont systemFontOfSize:16];
    lbl.textColor = [UIColor grayColor];
    [self addSubview:lbl];
    
    
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 150, ViewWidth, 95)];
    view2.backgroundColor = [UIColor whiteColor];
    [self addSubview:view2];
    
    
    //中间的view
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth/2, 170, 2, 55)];
    view3.backgroundColor = BACKGROUND2;
    [self addSubview:view3];
    
    
    
    //设置订单数目
//    self.lableNumer = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/4-10, 170, 60, 30)];
    
    
    self.lableNumer = [[UICountingLabel alloc]initWithFrame:CGRectMake(ViewWidth/4-26, 170, 60, 30)];
    self.lableNumer.textAlignment = NSTextAlignmentCenter;
    self.lableNumer.text = @"";
    [self addSubview:self.lableNumer];
    self.lableNumer.font = [UIFont systemFontOfSize:19];
    
    //设置营业额

//    self.labelPrice = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/4 * 3- 14, 170, 60, 30)];
    self.labelPrice = [[UICountingLabel alloc]initWithFrame:CGRectMake(ViewWidth/4 * 3 - 32, 170, 78, 30)];
    self.labelPrice.textAlignment = NSTextAlignmentCenter;
    self.labelPrice.text = @"";
    [self addSubview:self.labelPrice];
    self.labelPrice.font = [UIFont systemFontOfSize:19];
    
    
    
    //设置订单数目
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/6+6, 205, ViewWidth/6+40, 30)];
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.text = @"今日订单";
    lbl1.font = [UIFont systemFontOfSize:15];
    lbl1.textColor = [UIColor lightGrayColor];
    
    [self addSubview:lbl1];
    
    
    
    //设置今日金额
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/6 *4, 205, ViewWidth/6+20+40, 30)];
    lbl2.backgroundColor = [UIColor clearColor];
    lbl2.text = @"今日营业额";
    lbl2.font = [UIFont systemFontOfSize:15];
    lbl2.textColor = [UIColor lightGrayColor];
    
    [self addSubview:lbl2];
    

    
    [self sendAPI];
 
    
}






-(void)sendAPI
{
    
    
    //获得当前的时间
    NSString *date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    date = [formatter stringFromDate:[NSDate date]];
    
    
    
    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@business_report",SERVER_ADDRESS2];

    NSDictionary *params =@{@"wTel" : self.datasingleton.phone , @"uToken" : self.datasingleton.uToken ,@"report_time" : date};
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
       
        
        if ([responseObject[@"error_flag"] intValue] == 0)
        {
          
        
//            self.lableNumer.text = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_order"];
            
            int number = [[[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_order"] intValue ];
            
            //整数进行改变数字
            self.lableNumer.method = UILabelCountingMethodLinear;
            self.lableNumer.format = @"%d";
            [self.lableNumer countFrom:0 to:number withDuration:0.5];
            
            
            
//            
//            self.labelPrice.text = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_amount"];
            NSString *amount = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_amount"];
            NSString *amount1 = [NSString stringWithFormat:@"%.2f",[amount floatValue]];
//            NSLog(@"dhasdhashdk   %@",amount1);
            self.labelPrice.format = @"%.2f";
            
            [ self.labelPrice countFrom:0.00 to:[amount1 floatValue] withDuration:0.5];

            
            
            
        }
        
        else
        {
        
            
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
       
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];
    

}


@end
